import java.util.Scanner;

public class NGuessNumber {

    public static int[] generateAnswer(){
        int[] arr = new int[4];
        int index = 0;
        while(true){
            int random = (int)(Math.random() * 9);
            boolean isRepeat = false;
            for (int anInt : arr) {
                if (random == anInt) {
                    isRepeat = true;
                    break;
                }
            }
            if(isRepeat){
                continue;
            }
            arr[index] = random;
            index++;
            if(index == 4){
                break;
            }
        }
        return arr;
    }


    public static int[] getPlayerInput(){
        Scanner scanner = new Scanner(System.in);
        while(true){
            int[] arr = new int[4];
            boolean isRepeat = false;
            for(int temp = 0;temp < arr.length;temp++){
                arr[temp] = scanner.nextInt();
            }
            for (int j = 1; j < arr.length; j++) {
                for (int i = 0; i < j; i++) {
                    if (arr[j] == arr[i]) {
                        System.out.println("请输入四个不重复的数字");
                        isRepeat = true;
                    }
                }
            }
            if(isRepeat){
                continue;
            }
            return arr;
        }

    }

    public static String compareGuessAnswer(int[] input,int[] answer){
        int A = 0;
        int B = 0;
        for(int temp = 0;temp < input.length;temp++){
            if(input[temp] == answer[temp]){
                A++;
                continue;
            }
            for(int temp2 = 0;temp2 < answer.length;temp2++){
                if(input[temp] == answer[temp2]){
                    B++;
                    break;
                }
            }
        }
        System.out.println(A + "A" + B + "B");
        return (A + "A" + B + "B");
    }

    public static boolean isWin(String jg){
        return jg.equals("4A0B");
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        boolean isFirst = true;
        while(true){
            if(isFirst){
                System.out.println("是否开始游戏(y/n)");
            }else{
                System.out.println("是否继续游戏(y/n)");

            }
            String is = scanner.next();
            if(is.equals("n")){
                break;
            }else if(!is.equals("y")){
                System.out.println("请勿输入y/n以外的字符");
                continue;
            }
            isFirst = false;
            int[] answer = generateAnswer();
            for(int count = 0;count < 7;count++){
                System.out.println("你还有"+(7-count)+"次机会");
                int[] input = getPlayerInput();
                if(isWin(compareGuessAnswer(input,answer))){
                    System.out.println("你赢了");
                    break;
                }else{
                    System.out.println("猜错了,再试一次吧");
                }
            }
        }
    }
}
